# import cv2
from tkinter import messagebox
from tkinter import filedialog
from tkinter import *
from PIL import Image, ImageTk
import tkinter as tk
from _ast import If
import numpy as np
import os
#####################显示摄像头画面#####################
def start_camera():
    #启动 all_in_one
    os_str = 'python all_in_one.py'
    # os_str = 'all_in_one.exe'
    f = os.popen(os_str, 'r')
    global res
    res = f.readlines()
    f.close()
    #print(res)
root = tk.Tk()
root.geometry('1000x500')  # 设置窗口大小
root.resizable(0,0)  # 设置窗口最大及最小size
root.title('多余物识别定位')
# root.iconbitmap()只能是iso格式的图标       
leftPath = StringVar()
rightPath = StringVar()
classPath = StringVar()
imgleft = None
imgright = None
imgclass = None
# 照片存储路径
# 具体使用相对路径还是绝对路径需要进一步考虑
folder = "D:/python_camera/" 
def saveCallBack():
    global img_openleft, imgleft, img_openright, imgright, img_openclass, imgclass
    leftPath = folder + "left_0.jpg"
    rightPath = folder + "right_0.jpg"
    classPath = folder + "class_0.jpg"
    img_openleft = Image.open(leftPath).resize((320,180))
    img_openright = Image.open(rightPath).resize((320,180))
    img_openclass = Image.open(classPath).resize((320,180))
    root.imgleft = ImageTk.PhotoImage(img_openleft)
    root.imgright = ImageTk.PhotoImage(img_openright)
    root.imgclass = ImageTk.PhotoImage(img_openclass)    
    L1.configure(image = root.imgleft)
    L2.configure(image = root.imgright)
    L3.configure(image = root.imgclass)
    L4.configure(text = res)       
# pack是竖排居中，grid是自由定义
# tk.Label(self, text= '123', fg= 'red', bg= 'green', width= 100).grid(row= 0,column=0) #标签参数
# tk.Entry(self, textvariable= self.envar, fg= '#000',bg= '#ff0', width= '52').grid(row= 0,column=1) #编辑框参数
# text = tk.Text(self, fg= '#ff0', bg= '#000', width= 12) #文本框参数
# text.grid(columnspan= 2, rowspan= 2, pady= 10, padx= 500, ipadx= 500)#pady像素行距,padx不填充像素宽度,ipadx填充宽度
# text.insert('end', '123')
# 启动摄像头按钮，点击则弹出窗口显示摄像头画面
frame1 = Frame(root)
# Label(frame1, text= '图片读取地址：').grid(row= 0, column= 0)  # 标签，用grid便于后期管理，pack只是丢上去不管了
# E1 = Entry(frame1, width= 50, bg= 'green')
# E1.grid(row= 0, column= 1)
Button(frame1, text= '启动摄像头', width= 10, command= lambda:start_camera()).grid(row= 0, column= 2, padx= 10)
frame1.grid(padx= 450, pady= 5)    
frame2 = Frame(root)
# Label(frame2, text= '显示拍摄结果').grid(row= 0, column= 0)  # 标签，用grid便于后期管理，pack只是丢上去不管了
# E2 = Entry(frame2, width= 50, bg= 'red')
# E2.grid(row= 0, column= 1)
Button(frame2, text= '显示拍摄结果', width= 10, command= lambda:saveCallBack()).grid(row= 0, column= 2, padx= 10)
frame2.grid(padx= 200, pady= 10)
frame3 = Frame(root)
Label(frame3, text= '拍摄画面左').grid(row= 0, column= 0)
L1 = Label(frame3)
L1.grid(row= 1, column= 0)
Label(frame3, text= '拍摄画面右').grid(row= 0, column= 1)
L2 = Label(frame3)
L2.grid(row= 1, column= 1)
frame3.grid(pady= 10)
Label(frame3, text= '识别结果').grid(row= 0, column= 2)
L3 = Label(frame3)
L3.grid(row= 1, column= 2)
frame4 = Frame(root)
Label(frame4, text= '定位结果').grid(row= 0, column= 1)
L4 = Label(frame4, width= 50)
L4.grid(row= 1, column= 1)
frame4.grid(pady= 10)    
root.mainloop()